package com.xiangban.yanglaodajie_admin.service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiangban.yanglaodajie_admin.dao.ShopExpandMapper;
import com.xiangban.yanglaodajie_admin.entity.Honor;
import com.xiangban.yanglaodajie_admin.entity.Ids;
import com.xiangban.yanglaodajie_admin.entity.Leader;
import com.xiangban.yanglaodajie_admin.entity.dto.ShopExpandDto;
import com.xiangban.yanglaodajie_admin.entity.res.ApiResultCode;
import com.xiangban.yanglaodajie_admin.exception.BizException;
import com.xiangban.yanglaodajie_admin.utils.CommonUtils;
import com.xiangban.yanglaodajie_admin.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.xiangban.yanglaodajie_admin.utils.CommonUtils.BizEmpty;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/7/22 15:39
 */
@Service
public class ShopExpandService {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private CommonUtils commonUtils;
    @Autowired
    private ShopExpandMapper shopExpandMapper;
    @Autowired
    private PageUtil pageUtil;

    /**查看拓展信息*/
    public Object getShopExpand() {
        if (BizEmpty(commonUtils.getShopId(request))){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("shopId",Integer.parseInt(commonUtils.getShopId(request)));

        ShopExpandDto shopExpandDto = shopExpandMapper.selectShopExpand(map);
        if (shopExpandDto != null){
            String synopsisImage = shopExpandDto.getSynopsisImage();
            shopExpandDto.setSynopsisImages((List<String>) JSON.parse(synopsisImage));
            shopExpandDto.setSynopsisImage(null);
            return shopExpandDto;
        }else {
            return new ShopExpandDto();
        }

    }

    /**添加或修改拓展信息*/
    @Transactional
    public void addOrUpdShopExpand(ShopExpandDto shopExpandDto) {
        if (BizEmpty(commonUtils.getShopId(request))){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        shopExpandDto.setShopId(Integer.parseInt(commonUtils.getShopId(request)));

        HashMap<String, Object> map = new HashMap<>();
        map.put("shopId",Integer.parseInt(commonUtils.getShopId(request)));
        //是否存在拓展信息
        ShopExpandDto shopExpandDto1 = shopExpandMapper.selectShopExpand(map);

        if (shopExpandDto1 == null){
            Boolean aBoolean = checkRequirement(shopExpandDto);
            if (aBoolean){
                shopExpandDto.setSynopsisImage(JSON.toJSONString(shopExpandDto.getSynopsisImages()));
                shopExpandMapper.insertShopExpand(shopExpandDto);
            }else {
                throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
            }
        }else {
            //TODO 修改拓展信息
            if (!BizEmpty(shopExpandDto.getSynopsisImages())){
                shopExpandDto.setSynopsisImage(JSON.toJSONString(shopExpandDto.getSynopsisImages()));
            }
            shopExpandMapper.updateShopExpand(shopExpandDto);
        }
    }
    private Boolean checkRequirement(ShopExpandDto shopExpandDto){
        if (BizEmpty(shopExpandDto.getSynopsisImages())){
            return false;
        }
//        if (BizEmpty(shopExpandDto.getLeaderList())){
//            return false;
//        }
//        if (BizEmpty(shopExpandDto.getHonorList())){
//            return false;
//        }
        if (BizEmpty(shopExpandDto.getSynopsisText())){
            return false;
        }
        if (BizEmpty(shopExpandDto.getReportRequire())){
            return false;
        }
        if (BizEmpty(shopExpandDto.getEmail())){
            return false;
        }
        if (BizEmpty(shopExpandDto.getUrl())){
            return false;
        }
        if (BizEmpty(shopExpandDto.getPublicUrl())){
            return false;
        }
        if (BizEmpty(shopExpandDto.getPublicUrlLogo())){
            return false;
        }
        if (BizEmpty(shopExpandDto.getPublicUrlText())){
            return false;
        }
        return true;
    }

    /**领导列表*/
    public PageInfo<Leader> getLeaderList(Leader leader) {
        if (BizEmpty(commonUtils.getShopId(request))){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        PageHelper.startPage(pageUtil.getPage().getPageNum(),pageUtil.getPage().getPageSize());
        leader.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        List<Leader> leaders = shopExpandMapper.selectLeaderList(leader);
        if (BizEmpty(leaders)){
            return new PageInfo<>();
        }
        return  new PageInfo<>(leaders);
    }

    /**
     * @param leader 参数:商家id、领导id
     * @return领导详情
     */
    public Leader getLeaderInfo(Leader leader) {
        if (BizEmpty(commonUtils.getShopId(request))){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(leader.getId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        leader.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        return shopExpandMapper.selectLeader(leader);
    }

    /**添加或修改学校领导*/
    public void addOrUpdLeader(Leader leader) {
        if (BizEmpty(commonUtils.getShopId(request))){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        leader.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        if (leader.getId() == null){
            Boolean aBoolean = checkRequire(leader);
            if (aBoolean){
                shopExpandMapper.insertLeader(leader);
            }else {
                throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
            }
        }else {
            shopExpandMapper.updateLeader(leader);
        }
    }
    private Boolean checkRequire(Leader leader){
        if (BizEmpty(leader.getLeaderImage())){
            return false;
        }
        if (BizEmpty(leader.getLeaderName())){
            return false;
        }
        if (BizEmpty(leader.getPosition())){
            return false;
        }
        if (BizEmpty(leader.getSort())){
            return false;
        }
        return true;
    }

    /**删除学校领导*/
    public void delLeader(Ids ids) {
        if (BizEmpty(commonUtils.getShopId(request))){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(ids.getId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        ids.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        shopExpandMapper.deleteLeader(ids);
    }

    /**学校荣誉列表*/
    public PageInfo<Honor> getHonorList() {
        if (BizEmpty(commonUtils.getShopId(request))){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        PageHelper.startPage(pageUtil.getPage().getPageNum(),pageUtil.getPage().getPageSize());
        List<Honor> honors = shopExpandMapper.selectHonorList(Integer.parseInt(commonUtils.getShopId(request)));
        if (honors.size()<1){
            return new PageInfo<>();
        }
        return new PageInfo<>(honors);
    }

    /**
     * @param honor 参数：商家id、荣誉id
     * @return荣誉详情
     */
    public Honor getHonorInfo(Honor honor) {
        if (BizEmpty(commonUtils.getShopId(request))){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(honor.getId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        honor.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        return shopExpandMapper.selectHonorInfo(honor);
    }

    /**
     * @param honor 参数:商家id、荣誉id
     */
    @Transactional
    public void addOrUpdHonor(Honor honor) {
        if (BizEmpty(commonUtils.getShopId(request))){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        honor.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        if (honor.getId() == null){
            if (BizEmpty(honor.getHonorText())){
                throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
            }
            if (BizEmpty(honor.getHonorTime())){
                throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
            }
            shopExpandMapper.insertHonor(honor);
        }else {
            shopExpandMapper.updateHonor(honor);
        }

    }

    public void delHonor(Ids ids) {
        if (BizEmpty(commonUtils.getShopId(request))){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(ids.getId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        ids.setShopId(Integer.parseInt(commonUtils.getShopId(request)));
        shopExpandMapper.deleteHonor(ids);
    }
}
